//
//  String+Extensions.swift
//  JT-Architecture
//
//  Created by 果咱 on 2021/1/18.
//

import Foundation
import UIKit

extension String {
    
    /// 获取字体高度
    ///
    /// - Parameters:
    ///   - text: 文本
    ///   - availableWidth: 文本宽度
    /// - Returns: 字体高度
    public func sizeForText(font: UIFont, availableWidth: CGFloat) -> CGSize {
        let textSize = CGSize(width: availableWidth, height: CGFloat.greatestFiniteMagnitude)
        let attribute = [NSAttributedString.Key.font: font]
        let text = self
        let rect = text.boundingRect(with: textSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attribute, context: nil)
        return rect.size
    }
}

extension NSAttributedString {
    func stringWidth(fontSize:CGFloat,height:CGFloat,lineSpacing:CGFloat) -> CGFloat {
        let text = self.string as NSString
        let paraph = NSMutableParagraphStyle()
        paraph.lineSpacing = lineSpacing
        let attributes = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: fontSize),NSAttributedString.Key.paragraphStyle:paraph]
        let option = NSStringDrawingOptions.usesLineFragmentOrigin
        let rect : CGRect = text.boundingRect(with: CGSize(width:  CGFloat(MAXFLOAT), height: height), options: option, attributes: attributes, context: nil)
        
        return rect.size.width
    }
}
